﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class NghiPhepDAO
    {
        public static bool Insert(NghiPhepDTO np)
        {
            bool result = false;

            string strSQLEmployee = "Insert into NghiPhep " +
                "values (@Nhan,@Ngay,@So)";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@Nhan", np.MaNhanVien));
                sqlParams.Add(new SqlParameter("@Ngay", np.NgayBatDauNghi));
                sqlParams.Add(new SqlParameter("@So", np.SoNgayNghi));
                // Call execute none query

                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);//.ExecuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static List<NghiPhepDTO> SelectAll(string Ngay)
        {
            List<NghiPhepDTO> list = new List<NghiPhepDTO>();
            String strSql = "select * from NghiPhep np,NhanVien nv where np.NhanVien=nv.MaNV and " +
                "NgayBatDauNghi<='" + Ngay + "' and DATEADD(Day,SoNgayNghi,NgayBatDauNghi)>='" + Ngay + "'";

            try
            {
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    NghiPhepDTO np = new NghiPhepDTO();
                    np.MaNghiPhep = int.Parse(dr["MaNghiPhep"].ToString());
                    np.TenNhanVien = dr["HoTen"].ToString();
                    np.MaNhanVien= int.Parse(dr["NhanVien"].ToString());
                    np.NgayBatDauNghi = DateTime.Parse(dr["NgayBatDauNghi"].ToString()).ToString("dd/MM/yyyy");
                    np.SoNgayNghi = int.Parse(dr["SoNgayNghi"].ToString());
                    list.Add(np);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
    }
}
